// const fs = require("fs-extra");
import fs from "fs-extra";
// const handlebars = require("handlebars");
import handlebars from "handlebars";
// const { resolve } = require("path");
import { resolve } from "path";

import path from "path";
import { fileURLToPath } from "url";
const __filenameNew = fileURLToPath(import.meta.url);
const __dirnameNew = path.dirname(__filenameNew);

const getTplFilePath = (meta) => ({
  // docs 目录
  readme: {
    from: "./.template/docs/README.md.tpl",
    to: `../../packages/${meta.compName}/docs/README.md`,
  },
  demo: {
    from: "./.template/docs/demo.vue.tpl",
    to: `../../packages/${meta.compName}/docs/demo.vue`,
  },
  // src 目录
  vue: {
    from: "./.template/src/index.vue.tpl",
    to: `../../packages/${meta.compName}/src/${meta.compName}.vue`,
  },
  ts: {
    from: "./.template/src/index.ts.tpl",
    to: `../../packages/${meta.compName}/src/${meta.compName}.ts`,
  },
  // 根目录
  install: {
    from: "./.template/index.ts.tpl",
    to: `../../packages/${meta.compName}/index.ts`,
  },
});

const compFilesTplReplacer = (meta) => {
  const filePaths = getTplFilePath(meta);
  Object.keys(filePaths).forEach((key) => {
    const fileTpl = fs.readFileSync(
      resolve(__dirnameNew, filePaths[key].from),
      "utf-8"
    );
    const fileContent = handlebars.compile(fileTpl)(meta);
    fs.outputFile(
      resolve(__dirnameNew, filePaths[key].to),
      fileContent,
      (err) => {
        if (err) console.log(err);
      }
    );
  });
};

// 读取 packages/list.json 并更新
const listJsonTplReplacer = (meta) => {
  const listFilePath = "../../packages/list.json";
  const listFileTpl = fs.readFileSync(
    resolve(__dirnameNew, listFilePath),
    "utf-8"
  );
  const listFileContent = JSON.parse(listFileTpl);
  listFileContent.push(meta);
  const newListFileContentFile = JSON.stringify(listFileContent, null, 2);
  fs.writeFile(
    resolve(__dirnameNew, listFilePath),
    newListFileContentFile,
    (err) => {
      if (err) console.log(err);
    }
  );
  return listFileContent;
};

// 更新 router.ts
const routerTplReplacer = (listFileContent) => {
  const routerFileFrom = "./.template/router.ts.tpl";
  const routerFileTo = "../../src/router.ts";
  const routerFileTpl = fs.readFileSync(
    resolve(__dirnameNew, routerFileFrom),
    "utf-8"
  );
  const routerMeta = {
    routes: listFileContent.map((comp) => {
      return `{
    title: '${comp.compZhName}',
    name: '${comp.compName}',
    path: '/components/${comp.compName}',
    component: () => import('packages/${comp.compName}/docs/README.md'),
  }`;
    }),
  };
  const routerFileContent = handlebars.compile(routerFileTpl, {
    noEscape: true,
  })(routerMeta);
  fs.outputFile(
    resolve(__dirnameNew, routerFileTo),
    routerFileContent,
    (err) => {
      if (err) console.log(err);
    }
  );
};

// 更新 install.ts
const installTsTplReplacer = (listFileContent) => {
  const installFileFrom = "./.template/install.ts.tpl";
  const installFileTo = "../../packages/index.ts"; // 这里没有写错，别慌
  const installFileTpl = fs.readFileSync(
    resolve(__dirnameNew, installFileFrom),
    "utf-8"
  );
  const installMeta = {
    importPlugins: listFileContent
      .map(
        ({ compName }) => `import { ${compName}Plugin } from './${compName}';`
      )
      .join("\n"),
    installPlugins: listFileContent
      .map(({ compName }) => `${compName}Plugin.install?.(app);`)
      .join("\n    "),
    exportPlugins: listFileContent
      .map(({ compName }) => `export * from './${compName}'`)
      .join("\n"),
  };
  const installFileContent = handlebars.compile(installFileTpl, {
    noEscape: true,
  })(installMeta);
  fs.outputFile(
    resolve(__dirnameNew, installFileTo),
    installFileContent,
    (err) => {
      if (err) console.log(err);
    }
  );
};

const func = (meta) => {
  compFilesTplReplacer(meta);
  const listFileContent = listJsonTplReplacer(meta);
  routerTplReplacer(listFileContent);
  installTsTplReplacer(listFileContent);

  console.log(`组件新建完毕，请前往 packages/${meta.compName} 目录进行开发`);
};
export default func;
